#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include "RabinKarp.h"


void processText( const string& inputPath, const string& word, RabinCarp& carp, vector<int>& matches) {

	ifstream file(inputPath.c_str());
	if (file.is_open())
	{
		string line;
		int relativePos = 0;
		while ( file.good() )
		{
			getline(file, line);
			if ( line.length() > 0 ) {
				line.append("\n");
				carp.getNextMatches( line, word, matches, relativePos );
				relativePos = relativePos + line.length();
			}
		}
		file.close();
	} else {
		printf("Error file\n");
		getchar();
		exit(0);
	}
}


int main() {

	string word;
	string filePath;

	printf("Print substring:\n");
	std::getline(std::cin, word);
	printf("Print text file path:\n");
	std::getline(std::cin, filePath);
	
	RabinCarp capr;
	vector<int> matches;
	processText( filePath, word, capr, matches );

	printf("Matches:\n");
	int k = matches.size();
	if ( k > 10 ) {
		k = 10;
	}
	for ( int i = 0; i < k; ++i ) {
		printf("%d\n", matches[i]);
	}

	getchar();
	return 0;
}